#include "verilated_vcd_c.h" //可选，如果要导出vcd则需要加上
#include "Vysyx_20220337_Decode.h"
#include "stdio.h"
#include <stdlib.h>

vluint64_t main_time = 0;  //initial 仿真时间
 
double sc_time_stamp()
{
    return main_time;
}


/***********************************/
/**********Instruction Mem**********/
/***********************************/
uint32_t pmem_read (uint32_t i){
    uint32_t imem[] = { 0x00000413,
                        0x00009117,
                        0xffc10113,
                        0x00c000ef,
                        0x00000513,
                        0x00008067,
                        0xff010113,
                        0x00000517,
                        0x01c50513,
                        0x00113423,
                        0xfe9ff0ef,
                        0x00050513,
                        0x00100073,
                        0x0000006f };
    uint32_t addr = (i-0x80000000)/4;
    //std::cout<<"pc = "<<std::hex<<i<<"    "<<"inst = "<<std::hex<<imem[addr]<<std::endl;
    printf("pc = %016x instr = 0x%08x\n",i,imem[addr]);
    return imem[addr];
}


int main(int argc, char **argv)
{
    Verilated::commandArgs(argc, argv); 
    Verilated::traceEverOn(true); //导出vcd波形需要加此语句
 
    VerilatedVcdC* tfp = new VerilatedVcdC; //导出vcd波形需要加此语句
 
    Vysyx_20220337_Decode *top = new Vysyx_20220337_Decode("ysyx_20220337_Decode"); //调用VAccumulator.h里面的IO struct //实例化top模块
 
    top->trace(tfp, 0);   
    tfp->open("wave.vcd"); //打开vcd
    
    int addr = 0x80000000;
    while (sc_time_stamp() < 20 && !Verilated::gotFinish()) { //控制仿真时间
        switch (main_time)
        {
        case 4:
            top->instr = pmem_read(addr) ; // addi	a0,a0,1802
            addr = addr + 4 ;
            break;
        case 6:
            top->instr = pmem_read(addr) ; // addi	a0,a0,1536
            addr = addr + 4 ;	
            break;
        
        default:
            top->instr = 0;
            break;
        }
        
		top->eval();
		//printf("instr = 0x%016x\n", top->instr);
		tfp->dump(main_time); //dump wave
        main_time++; //推动仿真时间
    }
    top->final();
    tfp->close();
    delete top;
 
    return 0;
}